package com.tianya.common.action;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import com.tianya.common.util.SessionConst;
import org.apache.struts2.ServletActionContext;

@SuppressWarnings("serial")
public class LoginAction extends BaseAction {
	private String username;
	private String password;
	private String safecode;
	private String code;
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	@Override
	public String actionExecute() throws Exception 
	{
		//如果COOKICES保存用户名，读取
		HttpServletRequest request = ServletActionContext.getRequest(); 
		Cookie[] cookies =  request.getCookies();
		if( cookies!= null)
		{
			for(int i = 0; i < cookies.length;i++)
			{
			  if("USERNAME".equals(cookies[i].getName()))
			  {
				  setUsername(cookies[i].getValue());

				  break;
			  }
			}
		}
		
		//登录成功，用户存入SESSION
		session.removeAttribute(SessionConst.User);
		//用户权限存入Session
		session.removeAttribute(SessionConst.UserPermissions);
		//用户信息和设置
		session.removeAttribute(SessionConst.UserInfo);

		return SUCCESS;
	}
	
	
//	@Override
//	public String actionInit() throws Exception {
//		// TODO Auto-generated method stub
//		if(!Utility.isEmpty(code)){
//			if("E00004".equals(code)){
//				setMessage(MessageType.Error,code,new String[]{"用户名或密码"});
//			}
//			if("E00003".equals(code)){
//				setMessage(MessageType.Error,"E00003",new String[]{"用户"});
//			}
//			if("E00008".equals(code)){
//				setMessage(MessageType.Error,"E00008");
//			}
//			
//		}
//		return SUCCESS;
//	}
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getSafecode() {
		return safecode;
	}

	public void setSafecode(String safecode) {
		this.safecode = safecode;
	}
}
